home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 1.iso / dist / fw_kdelibs.idb / usr / freeware / kde / include / jsexec.h.z / jsexec.h
C/C++ Source or Header  |  1999-01-26  |  11KB  |  391 lines

  1. /* This file is part of the KDE libraries
  2.     Copyright (C) 1997 Torben Weis (weis@kde.org)
  3.  
  4.     This library is free software; you can redistribute it and/or
  5.     modify it under the terms of the GNU Library General Public
  6.     License as published by the Free Software Foundation; either
  7.     version 2 of the License, or (at your option) any later version.
  8.  
  9.     This library is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.     Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public License
  15.     along with this library; see the file COPYING.LIB.  If not, write to
  16.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.     Boston, MA 02111-1307, USA.
  18. */
  19. #ifndef JSEXEC_H
  20. #define JSEXEC_H
  21.  
  22. class JSObject;
  23. class JSScope;
  24. class JSVariableObject;
  25. class JSFunctionObject;
  26. class JSUserDefinedObject;
  27. class JSInstanceScope;
  28.  
  29. #include <qlist.h>
  30. #include <qstack.h>
  31.  
  32. #include "jstree.h"
  33. #include "jserror.h"
  34.  
  35. #define TYPE_JSObject 1
  36. #define TYPE_JSIntegerObject 2
  37. #define TYPE_JSStringObject 3
  38. #define TYPE_JSVariableObject 4
  39. #define TYPE_JSParameterListObject 5
  40. #define TYPE_JSFunctionObject 6
  41. #define TYPE_JSUserDefinedObject 7
  42. #define TYPE_JSScope 8
  43. #define TYPE_JSInstanceScope 9
  44. #define TYPE_JSBoolObject 10
  45. #define TYPE_JSFloatObject 11
  46. #define TYPE_JSAbstractArrayObject 12
  47.  
  48. typedef QList<JSNode> JSCode;
  49.  
  50. int execJavaScript( JSCode *_code, JSScope *_global_scope, JSInstanceScope *_instanceScope );
  51. int parseJavaScript( const char *_script, JSCode* _code, JSScope* _global );
  52. JSScope* makeGlobalScope();
  53.  
  54. class JSValue
  55. {
  56. public:
  57.     JSValue();
  58.     virtual ~JSValue();
  59.     
  60.     bool isLeftValue() { return bLeftValue; }
  61.     void setLeftValue( bool _b ) { bLeftValue = _b; }
  62.     
  63.     JSObject *getObject() { return object; }
  64.     void setObject( JSObject *_o );
  65.     
  66.     void setAutoDelete( bool _b ) { bAutoDelete = _b; }
  67.     bool isAutoDelete() { return bAutoDelete; }
  68.     
  69. protected:
  70.     JSObject *object;
  71.     bool bAutoDelete;
  72.     bool bLeftValue;
  73. };
  74.  
  75. class JSObject
  76. {
  77. public:
  78.     JSObject() { }
  79.     virtual ~JSObject() { }
  80.  
  81.     virtual int isA() { return TYPE_JSObject; }    
  82.     virtual bool inherits( int _type ) { return ( _type == TYPE_JSObject ); }
  83.  
  84.     virtual JSObject* copy() { return new JSObject(); }    
  85.     virtual void release() { delete this; }
  86. };
  87.  
  88. /**********************************************************
  89.  *
  90.  * JSScope
  91.  *
  92.  *********************************************************/
  93.  
  94. class JSScope
  95. {
  96. public:
  97.     JSScope();
  98.     virtual ~JSScope() { }
  99.   
  100.     virtual int isA() { return TYPE_JSScope; }    
  101.     
  102.     void insertObject( JSObject* _obj );
  103.  
  104.     JSVariableObject* findVariable( const char *_name );
  105.     JSFunctionObject* findFunction( const char *_name );
  106.     
  107.     QList<JSObject>* getObjectList() { return &objectList; }
  108.     
  109. protected:
  110.     QList<JSObject> objectList;
  111. };
  112.  
  113. /**********************************************************
  114.  *
  115.  * JSInstanceScope
  116.  *
  117.  *********************************************************/
  118.  
  119. class JSInstanceScope : public JSScope
  120. {
  121. public:
  122.     JSInstanceScope() { }
  123.     virtual ~JSInstanceScope() { }
  124.  
  125.     virtual int isA() { return TYPE_JSInstanceScope; }    
  126.  
  127.     void setObject( JSUserDefinedObject* _obj ) { object = _obj; }
  128.     
  129.     JSUserDefinedObject* getObject() { return object; }
  130.     
  131. protected:
  132.     JSUserDefinedObject *object;
  133. };
  134.  
  135. /**********************************************************
  136.  *
  137.  * JSScopeStack
  138.  *
  139.  *********************************************************/
  140.  
  141. class JSScopeStack
  142. {
  143. public:
  144.     JSScopeStack( JSScope *_globalScope, JSInstanceScope *_instanceScope = 0L );
  145.     virtual ~JSScopeStack();
  146.   
  147.     /**
  148.      * @param _top_only causes only the upper most scope to be searched.
  149.      * 
  150.      * @see JSIdentifier::leftValue
  151.      */
  152.     JSVariableObject* findVariable( const char *_name, bool _top_only = FALSE );
  153.     JSFunctionObject* findFunction( const char *_name );
  154.  
  155.     void pushScope( JSScope* _scope );
  156.     void popScope();
  157.     JSScope* topScope();
  158.     
  159.     void pushInstanceScope( JSInstanceScope* _scope );
  160.     void popInstanceScope();
  161.     JSInstanceScope* topInstanceScope();
  162.     
  163. protected:
  164.     QList<JSScope> scopeList;
  165.     QStack<JSInstanceScope> instanceScopeStack;
  166.  
  167.     /**
  168.      * If this pointer is NOT 0L, then this scope may not be deleted. Mention that all other
  169.      * scopes are deleted if @ref #scopeList is deleted.
  170.      */
  171.     JSScope* globalScope;    
  172. };
  173.  
  174. class JSUserDefinedObject : public JSObject
  175. {
  176. public:
  177.     JSUserDefinedObject();
  178.     virtual ~JSUserDefinedObject() { }
  179.  
  180.     virtual int isA() { return TYPE_JSUserDefinedObject; }    
  181.     virtual bool inherits( int _type )
  182.     { if ( _type == TYPE_JSUserDefinedObject ) return TRUE; else return JSObject::inherits( _type ); }
  183.  
  184.     virtual JSObject* copy() { lock++; return this; }
  185.     virtual void release() { if ( lock == 1 ) delete this; else lock--;  }
  186.     
  187.     JSInstanceScope* getScope() { return &scope; }
  188.     
  189. protected:
  190.     JSInstanceScope scope;
  191.     /**
  192.      * Amount of references hold to this object.
  193.      *
  194.      * @see copy
  195.      * @see release
  196.      */
  197.     int lock;
  198. };
  199.  
  200. /**
  201.  * @short Abstract class for Arrays.
  202.  *
  203.  * This class is only an abstract class. If your object is going to accept the "ident[ index ]"
  204.  * Operator then you have to derive from this class.
  205.  *
  206.  * @see JSAbstractArrayAccess
  207.  */
  208. class JSAbstractArrayObject : public JSUserDefinedObject
  209. {
  210. public:
  211.     JSAbstractArrayObject() { }
  212.     virtual ~JSAbstractArrayObject() { }
  213.  
  214.     virtual int isA() { return TYPE_JSAbstractArrayObject; }    
  215.     virtual bool inherits( int _type ) { if ( _type == TYPE_JSAbstractArrayObject ) return TRUE;
  216.                                          else return JSUserDefinedObject::inherits( _type ); }
  217.     
  218.     virtual int rightValue( JSObject *, JSValue * ) { return ERROR_JSNotARightValue; }
  219.     virtual int leftValue( JSObject *, JSValue * ) { return ERROR_JSNotALeftValue; }
  220. };
  221.  
  222. class JSStringObject : public JSUserDefinedObject
  223. {
  224. public:
  225.     JSStringObject( const char *_string );
  226.     virtual ~JSStringObject() { }
  227.  
  228.     virtual int isA() { return TYPE_JSStringObject; }    
  229.     virtual bool inherits( int _type ) { if ( _type == TYPE_JSStringObject ) return TRUE;
  230.                                          else return JSUserDefinedObject::inherits( _type ); }
  231.  
  232.     const char* getString() { return string.data(); }
  233.     QString& getQString() { return string; }
  234.  
  235. protected:
  236.     QString string;
  237. };
  238.  
  239. class JSParameterListObject : public JSObject
  240. {
  241. public:
  242.     JSParameterListObject();
  243.     virtual ~JSParameterListObject() { }
  244.  
  245.     virtual int isA() { return TYPE_JSParameterListObject; }    
  246.     virtual bool inherits( int _type ) { if ( _type == TYPE_JSParameterListObject ) return TRUE;
  247.                                          else return JSObject::inherits( _type ); }
  248.  
  249.     void appendValue( JSValue* _val ) { parameterValues.append( _val ); }
  250.  
  251.     JSValue* firstValue() { return parameterValues.first(); }
  252.     JSValue* nextValue() { return parameterValues.next(); }
  253.     
  254. protected:
  255.     QList<JSValue> parameterValues;
  256. };
  257.  
  258. class JSFunctionObject : public JSObject
  259. {
  260. public:
  261.     JSFunctionObject( JSFunction *_func );
  262.     virtual ~JSFunctionObject() { }
  263.  
  264.     virtual int isA() { return TYPE_JSFunctionObject; }    
  265.     virtual bool inherits( int _type ) { if ( _type == TYPE_JSFunctionObject ) return TRUE;
  266.                                          else return JSObject::inherits( _type ); }
  267.  
  268.     virtual JSObject* copy() { return new JSFunctionObject( function ); }    
  269.  
  270.     JSFunction* getFunction() { return function; }
  271.     const char* getName();
  272.     
  273.     void setObject( JSUserDefinedObject *_obj ) { object = _obj; }
  274.     JSUserDefinedObject* getObject() { return object; }
  275.     
  276. protected:
  277.     JSFunction *function;
  278.     JSUserDefinedObject *object;
  279. };
  280.  
  281. class JSVariableObject : public JSObject
  282. {
  283. public:
  284.     JSVariableObject();
  285.     virtual ~JSVariableObject();
  286.  
  287.     virtual int isA() { return TYPE_JSVariableObject; }    
  288.     virtual bool inherits( int _type ) { if ( _type == TYPE_JSVariableObject ) return TRUE;
  289.                                          else return JSObject::inherits( _type ); }
  290.  
  291.     virtual void setName( const char *_name ) {    if ( bConst ) return; name = _name; }
  292.     virtual const char* getName() { return name.data(); }
  293.  
  294.     virtual JSObject *getValue() { return value; }
  295.     virtual void setValue( JSObject* _val ) { if ( bConst ) return; value = _val; }
  296.  
  297.     virtual void clear();
  298.     
  299.     /**
  300.      * If a variable is const, then you can not assign anything to this variable. Use this to
  301.      * implement for example builtin objects like "document".
  302.      * This function is for extensions only, it is not called by the interpreter.
  303.      */
  304.     void setConst( bool _c ) { bConst = _c; }
  305.     bool isConst() { return bConst; }
  306.  
  307.     /**
  308.      * Set this flag if the value of this variable changes sometimes ( examples: time, status bar, ... ).
  309.      * If this flag is set, the object returned from @ref #getValue will be deleted if not
  310.      * further used.
  311.      */
  312.     void setDynamic( bool _c ) { bDynamic = _c; }
  313.     bool isDynamic() { return bDynamic; }
  314.     
  315. protected:
  316.     QString name;
  317.  
  318.     JSObject *value;
  319.  
  320.     bool bConst;
  321.     bool bDynamic;
  322. };
  323.  
  324. /* class JSStringObject : public JSObject
  325. {
  326. public:
  327.     JSStringObject( const char *_str );
  328.     virtual ~JSStringObject() { }
  329.  
  330.     virtual int isA() { return TYPE_JSStringObject; }    
  331.  
  332. protected:
  333.     QString string;
  334. }; */
  335.  
  336. class JSIntegerObject : public JSObject
  337. {
  338. public:
  339.     JSIntegerObject( int _i );
  340.     JSIntegerObject() { }
  341.  
  342.     virtual int isA() { return TYPE_JSIntegerObject; }    
  343.     virtual bool inherits( int _type ) { if ( _type == TYPE_JSIntegerObject ) return TRUE;
  344.                                          else return JSObject::inherits( _type ); }
  345.  
  346.     virtual JSObject* copy() { return new JSIntegerObject( value ); }    
  347.  
  348.     int getValue() { return value; }
  349.     
  350. protected:
  351.     int value;
  352. };
  353.  
  354. class JSBoolObject : public JSObject
  355. {
  356. public:
  357.     JSBoolObject( bool _i );
  358.     JSBoolObject() { }
  359.  
  360.     virtual int isA() { return TYPE_JSBoolObject; }    
  361.     virtual bool inherits( int _type ) { if ( _type == TYPE_JSBoolObject ) return TRUE;
  362.                                          else return JSObject::inherits( _type ); }
  363.  
  364.     virtual JSObject* copy() { return new JSBoolObject( value ); }    
  365.  
  366.     bool getValue() { return value; }
  367.     
  368. protected:
  369.     bool value;
  370. };
  371.  
  372. class JSFloatObject : public JSObject
  373. {
  374. public:
  375.     JSFloatObject( double _i );
  376.     JSFloatObject() { }
  377.  
  378.     virtual int isA() { return TYPE_JSFloatObject; }    
  379.     virtual bool inherits( int _type ) { if ( _type == TYPE_JSFloatObject ) return TRUE;
  380.                                          else return JSObject::inherits( _type ); }
  381.  
  382.     virtual JSObject* copy() { return new JSFloatObject( value ); }    
  383.  
  384.     double getValue() { return value; }
  385.     
  386. protected:
  387.     double value;
  388. };
  389.  
  390. #endif
  391.